﻿#include "stdafx.h"
#include "DmOptEx.h"
#include "..\GlobalFiles\ZLog.h"


ZDmOptEx::ZDmOptEx()
{
	WinExec("regsvr32.exe dm.dll /s",SW_SHOW);
	WinExec("regsvr32.exe kml.dll /s",SW_SHOW);

	CoInitialize(NULL);
	
	{
		CLSID clsid;
		HRESULT hr=CLSIDFromProgID(OLESTR("dm.dmsoft"),&clsid); //利用“根名称.类名”获取CLSID，&就是把指针给函数，也就是传址。 
		if(!m_dmSoft.CreateDispatch(clsid)) //从注册表中获取到dll路径，到这里就调用成功了。
		{
			AfxMessageBox(_T("大漠组件初始化失败"));
		}
	}

	{
		CLSID clsid;
		HRESULT hr=CLSIDFromProgID(OLESTR("MCUAPP.KML"),&clsid); //利用“根名称.类名”获取CLSID，&就是把指针给函数，也就是传址。 
		if(!m_kmlSoft.CreateDispatch(clsid)) //从注册表中获取到dll路径，到这里就调用成功了。
		{
			AfxMessageBox(_T("com组件初始化失败"));
		}
		if(m_kmlSoft.OpenDevice() == 0)
		{
			AfxMessageBox(_T("是不是忘了插设备了啊"));
		}
	}

	m_dmSoft.UnBindWindow();
}

ZDmOptEx& ZDmOptEx::instance()
{
	static ZDmOptEx dmOptEx;
	return dmOptEx;
}

HWND ZDmOptEx::GetMousePointWindow()
{
	return (HWND)m_dmSoft.GetMousePointWindow();
}
// 
// bool ZDmOptEx::BindWindow( HWND hBindWnd )
// {
// 	if(m_dmSoft.BindWindow((long)hBindWnd, _T("normal"), _T("normal"), _T("normal"), 0) == 1)
// 	{
// 		return true;
// 	}
// 
// 	ASSERT(FALSE);
// 	return false;
// }

void ZDmOptEx::KeyPress( long vk, int iSleepTime/* = 100 */)
{
#ifdef UseKML
	m_kmlSoft.KeyDown(_getKeyNameByVK(vk));
	Sleep(_getRandNum(100, 500));
	m_kmlSoft.KeyUp(_getKeyNameByVK(vk));
#else
	m_dmSoft.KeyPress(vk);
#endif
	Sleep(iSleepTime);
}

void ZDmOptEx::KeyDown( long vk )
{
#ifdef UseKML
	m_kmlSoft.KeyDown(_getKeyNameByVK(vk));
#else
	m_dmSoft.KeyDown(vk);
#endif
}

void ZDmOptEx::KeyUp( long vk )
{
#ifdef UseKML
	m_kmlSoft.KeyUp(_getKeyNameByVK(vk));
#else
	m_dmSoft.KeyUp(vk);
#endif
}

void ZDmOptEx::LeftClick(int iSleepTimeAfterClick /*= 100*/)
{
#ifdef UseKML
	m_kmlSoft.LeftClick(1);
#else
	m_dmSoft.LeftClick();
#endif
	Sleep(iSleepTimeAfterClick);
}

void ZDmOptEx::RightClick()
{
#ifdef UseKML
	m_kmlSoft.RightClick(1);
#else
	m_dmSoft.RightClick();
#endif
}

void ZDmOptEx::MiddleClick()
{
#ifdef UseKML
	m_kmlSoft.MiddleClick(1);
#else
	m_dmSoft.MiddleClick();
#endif
}

int ZDmOptEx::WheelUp( int iCount /*= 45*/ )
{
#ifdef UseKML
	m_kmlSoft.WheelUp(iCount);
#else
	m_dmSoft.WheelUp();
#endif
}

int ZDmOptEx::WheelDown( int iCount /*= 45*/ )
{
#ifdef UseKML
	m_kmlSoft.WheelDown(iCount);
#else
	m_dmSoft.WheelDown();
#endif
}

void ZDmOptEx::LeftDoubleClick()
{
#ifdef UseKML
	m_kmlSoft.LeftDoubleClick(1);
#else
	m_dmSoft.LeftDoubleClick();
#endif
}

void ZDmOptEx::LeftDown()
{
#ifdef UseKML
	m_kmlSoft.LeftDown();
#else
	m_dmSoft.LeftDown();
#endif
}

void ZDmOptEx::LeftUp()
{
#ifdef UseKML
	m_kmlSoft.LeftUp();
#else
	m_dmSoft.LeftUp();
#endif
}

void ZDmOptEx::RightDown()
{
#ifdef UseKML
	m_kmlSoft.RightDown();
#else
	m_dmSoft.RightDown();
#endif
}

void ZDmOptEx::RightUp()
{
#ifdef UseKML
	m_kmlSoft.RightUp();
#else
	m_dmSoft.RightUp();
#endif
}

void ZDmOptEx::MoveToLClick( const CPoint& pt )
{
	MoveTo(pt);
	Sleep(100);
	LeftClick();
	Sleep(100);
}

void ZDmOptEx::MoveTo( const CPoint& pt )
{
#ifdef UseKML
	m_kmlSoft.SimulationMove(pt.x, pt.y);
#else
	m_dmSoft.MoveTo(pt.x, pt.y);
#endif
}

void ZDmOptEx::MoveToSlow( const CPoint& pt, UINT delayTime_ms, UINT moveCount /*= 10*/ )
{
	ASSERT(FALSE);

	if(moveCount == 0 || delayTime_ms == 0)
	{
		ASSERT(FALSE);
		return;
	}

	int iMoveCount = delayTime_ms/moveCount + (delayTime_ms%moveCount == 0 ? 0 : 1);

// 	for (int i=0; i<iMoveCount; i++)
// 	{
// 		m_dmSoft.MoveTo(pt);
// 	}
}

bool ZDmOptEx::AppendFontDirc( const CString& sFontDircName )
{
	if (m_vecFontDircsName.size() > MAX_DIRC_COUNT)
	{
		return false;
	}

	if(0 == m_dmSoft.SetDict(m_vecFontDircsName.size(), sFontDircName))
	{
		CString sStr;
		sStr.Format(_T("字库%s打开失败"), sFontDircName);
		AfxMessageBox(sFontDircName);
		return false;
	}

	m_vecFontDircsName.push_back(sFontDircName);
//	AfxMessageBox(_T("打开字库成功"));
	return true;
}

CString ZDmOptEx::Ocr( const CRect& rect, const CString& strColor, double sim, int iTryTimes /*= 10*/ )
{
	CString sStr;

	for (int i=0; i<iTryTimes; i++)
	{
		sStr = m_dmSoft.Ocr(rect.left, rect.top, rect.right, rect.bottom, strColor, sim);
		if (!sStr.IsEmpty())
		{
			return sStr;
		}
	}
	return sStr;
}

bool ZDmOptEx::FindStr( const CRect& rect, const CString& str, const CString& strColor, double sim, CPoint& ltPt, int iTryTimes/* = 10 */)
{
	VARIANT x, y;

	int iRetVal = 0;
	for (int i=0; i<iTryTimes; i++)
	{
		iRetVal = m_dmSoft.FindStr(rect.left, rect.top, rect.right, rect.bottom, str, strColor, sim, &x, &y);
		if (iRetVal >= 0)
		{
			break;
		}
		Sleep(50);
	}
	
	ltPt.x = x.intVal;
	ltPt.y = y.intVal;

	CString sStr;
	sStr.Format(_T("‘%s’的位置%d-%d"), str, ltPt.x, ltPt.y);
	g_writeLog(sStr);

	return (-1 != iRetVal);
}

CString ZDmOptEx::_getKeyNameByVK( long vk )
{
	TCHAR keyName[30] = { 0 };
	GetKeyNameText((MapVirtualKey(vk,0)<<16), keyName, 30);
//	AfxMessageBox(keyName);
	return keyName;
}

int ZDmOptEx::_getRandNum( int iMinNum, int iMaxNum )
{
	int iNumCount = 1 + iMaxNum-iMinNum;
	if (iNumCount <= 1)
	{
		VERIFY(FALSE);
		return iMinNum;
	}

	return (iMinNum + GetTickCount() % iNumCount);
}
